﻿using System.Collections.Generic;

namespace dk.Helper.Base.Core
{
    public class Player : IPlayer
    {
        private int averageVillagePoints;

        /// <summary>
        /// Initializes a new instance of the <see cref="Player"/> class.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="name">The name.</param>
        /// <param name="allyID">The ally ID.</param>
        /// <param name="villageCount">The village count.</param>
        /// <param name="points">The points.</param>
        /// <param name="rank">The rank.</param>
        /// <param name="villages">The villages.</param>
        public Player(int id, string name, int allyID, int villageCount, int points, int rank, IList<IVillage> villages)
        {
            ID = id;
            Name = name;
            AllyID = allyID;
            VillageCount = villageCount;
            Points = points;
            Rank = rank;
            Villages = villages ?? new List<IVillage>();
        }

        #region IPlayer Members

        /// <summary>
        /// Gets the ID.
        /// </summary>
        public int ID { get; private set; }

        /// <summary>
        /// Gets the name.
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// Gets the ally ID.
        /// </summary>
        public int AllyID { get; private set; }

        /// <summary>
        /// Gets the village count.
        /// </summary>
        public int VillageCount { get; private set; }

        /// <summary>
        /// Gets the points.
        /// </summary>
        public int Points { get; private set; }

        /// <summary>
        /// Gets the rank.
        /// </summary>
        public int Rank { get; private set; }

        /// <summary>
        /// Gets the villages.
        /// </summary>
        public IList<IVillage> Villages { get; private set; }

        /// <summary>
        /// Gets the average village points.
        /// </summary>
        public int AverageVillagePoints
        {
            get
            {
                if (averageVillagePoints == 0)
                {
                    if (Villages.Count == 0)
                    {
                        averageVillagePoints = 0;
                        return averageVillagePoints;
                    }
                    int sumPoints = 0;
                    foreach (IVillage village in Villages)
                    {
                        sumPoints += village.Points;
                    }
                    averageVillagePoints = sumPoints/Villages.Count;
                }
                return averageVillagePoints;
            }
        }

        #endregion
    }
}